<?php
namespace common\modules\content;

class ModuleInfo extends \common\core\modularity\ModuleInfo
{
    public function init()
    {
        parent::init();
        
        $this->id = "content";
        $this->name = "内容";
        $this->description = '内容管理模块';
        $this->version = '1.0.0';
        $this->icon = '<i class="fa fa-edit"></i>';
        $this->has_backend = true;
        $this->has_frontend = true;
        $this->has_api = false;
        
        $this->is_system = false;
        
        $this->requireModules = 'user';
    }
    
    public function install()
    {
        $tableOptions = null;
        if ($this->db->driverName === 'mysql') {
            $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
        }
    
        $this->createTable('{{%widget_menu}}', [
            'id' => $this->primaryKey(),
            'key' => $this->string(32)
            ->notNull(),
            'title' => $this->string()
            ->notNull(),
            'items' => $this->text()
            ->notNull(),
            'status' => $this->smallInteger()
            ->notNull()
            ->defaultValue(0)
        ], $tableOptions);
        
        $this->createTable('{{%widget_text}}', [
            'id' => $this->primaryKey(),
            'key' => $this->string()
            ->notNull(),
            'title' => $this->string()
            ->notNull(),
            'body' => $this->text()
            ->notNull(),
            'status' => $this->smallInteger(),
            'created_at' => $this->integer(),
            'updated_at' => $this->integer()
        ], $tableOptions);
        
        $this->createIndex('idx_widget_text_key', '{{%widget_text}}', 'key');
        
        $this->createTable('{{%widget_carousel}}', [
            'id' => $this->primaryKey(),
            'key' => $this->string()
            ->notNull(),
            'status' => $this->smallInteger()
            ->defaultValue(0)
        ], $tableOptions);
        
        $this->createTable('{{%widget_carousel_item}}', [
            'id' => $this->primaryKey(),
            'carousel_id' => $this->integer()
            ->notNull(),
            'base_url' => $this->string(1024),
            'path' => $this->string(1024),
            'type' => $this->string(),
            'url' => $this->string(1024),
            'caption' => $this->string(1024),
            'status' => $this->smallInteger()
            ->notNull()
            ->defaultValue(0),
            'order' => $this->integer()
            ->defaultValue(0),
            'created_at' => $this->integer(),
            'updated_at' => $this->integer()
        ], $tableOptions);
        
        $this->addForeignKey('fk_item_carousel', '{{%widget_carousel_item}}', 'carousel_id', '{{%widget_carousel}}', 'id', 'cascade', 'cascade');
        
        $this->createTable('{{%article_category}}', [
            'id' => $this->primaryKey(),
            'slug' => $this->string(1024)
            ->notNull(),
            'title' => $this->string(512)
            ->notNull(),
            'body' => $this->text(),
            'parent_id' => $this->integer(),
            'status' => $this->smallInteger()
            ->notNull()
            ->defaultValue(0),
            'created_at' => $this->integer(),
            'updated_at' => $this->integer()
        ], $tableOptions);
        
        $this->createTable('{{%article}}', [
            'id' => $this->primaryKey(),
            'slug' => $this->string(1024)
            ->notNull(),
            'title' => $this->string(512)
            ->notNull(),
            'body' => $this->text()
            ->notNull(),
            'view' => $this->string(),
            'category_id' => $this->integer(),
            'thumbnail_base_url' => $this->string(1024),
            'thumbnail_path' => $this->string(1024),
            'author_id' => $this->integer(),
            'updater_id' => $this->integer(),
            'status' => $this->smallInteger()
            ->notNull()
            ->defaultValue(0),
            'published_at' => $this->integer(),
            'created_at' => $this->integer(),
            'updated_at' => $this->integer()
        ], $tableOptions);
        
        $this->createTable('{{%article_attachment}}', [
            'id' => $this->primaryKey(),
            'article_id' => $this->integer()
            ->notNull(),
            'path' => $this->string()
            ->notNull(),
            'base_url' => $this->string(),
            'type' => $this->string(),
            'size' => $this->integer(),
            'name' => $this->string(),
            'created_at' => $this->integer()
        ]);
        
        $this->addForeignKey('fk_article_attachment_article', '{{%article_attachment}}', 'article_id', '{{%article}}', 'id', 'cascade', 'cascade');
        $this->addForeignKey('fk_article_author', '{{%article}}', 'author_id', '{{%user}}', 'id', 'cascade', 'cascade');
        $this->addForeignKey('fk_article_updater', '{{%article}}', 'updater_id', '{{%user}}', 'id', 'set null', 'cascade');
        $this->addForeignKey('fk_article_category', '{{%article}}', 'category_id', '{{%article_category}}', 'id');
        $this->addForeignKey('fk_article_category_section', '{{%article_category}}', 'parent_id', '{{%article_category}}', 'id', 'cascade', 'cascade');
        
        $this->createTable('{{%page}}', [
            'id' => $this->primaryKey(),
            'slug' => $this->string(2048)
            ->notNull(),
            'title' => $this->string(512)
            ->notNull(),
            'body' => $this->text()
            ->notNull(),
            'view' => $this->string(),
            'status' => $this->smallInteger()
            ->notNull(),
            'created_at' => $this->integer(),
            'updated_at' => $this->integer()
        ], $tableOptions);
    }
    
    public function uninstall()
    {
        $this->dropTable('{{%page}}');
        
        $this->dropForeignKey('fk_article_attachment_article', '{{%article_attachment}}');
        $this->dropForeignKey('fk_article_author', '{{%article}}');
        $this->dropForeignKey('fk_article_updater', '{{%article}}');
        $this->dropForeignKey('fk_article_category', '{{%article}}');
        $this->dropForeignKey('fk_article_category_section', '{{%article_category}}');
        
        $this->dropTable('{{%article_attachment}}');
        $this->dropTable('{{%article}}');
        $this->dropTable('{{%article_category}}');
        
        $this->dropForeignKey('fk_item_carousel', '{{%widget_carousel_item}}');
        $this->dropTable('{{%widget_carousel_item}}');
        $this->dropTable('{{%widget_carousel}}');
        
        $this->dropTable('{{%widget_text}}');
        $this->dropTable('{{%widget_menu}}');
    }
}